package com.stxx.stiot2.service.tran;

import com.stxx.framwork.tran.TranService;
import com.stxx.framwork.utils.StoreUtils;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.sql.SqlBuilder;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DeviceTranService implements TranService {
    @Inject
    StoreUtils storeUtils;
    @Override
    public Map<String, Map<String, Object>> tran(String fieldName, Collection<String> ids) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.append("select t.device_id,t.name from ")
                .append(StoreUtils.getTableName("device")).append(" t")
                .append(" where t.device_id in(?...)", ids);
        Map<String, Map<String, Object>> map = new HashMap<>();
        try {
            List<Map> list1 = storeUtils.sql(sqlBuilder).queryRowList(Map.class);
            if (list1 != null) {
                list1.forEach(item -> {
                    Map<String, Object> tranMap = new HashMap<>();
                    tranMap.put(fieldName, item.get("name"));
                    map.put(item.get(fieldName).toString(), tranMap);
                });
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return map;
    }
}
